//
//  GTSettingViewController.m
//  GiftTalk
//
//  Created by Wan on 16/4/17.
//  Copyright © 2016年 Wan. All rights reserved.
//

#import "GTSettingViewController.h"
#import "GTSettingGroup.h"
#import "GTSettingCell.h"
#import "GTSettingItem.h"
#import "GTSettingSwitchItem.h"
#import "GTSettingLabelItem.h"
#import "GTSettingArrowItem.h"

@implementation GTSettingViewController

- (NSMutableArray *)dataList {
    if (!_dataList) {
        _dataList = [NSMutableArray array];
    }
    return _dataList;
}

- (void)setupDataList {
    GTSettingArrowItem *status = [GTSettingArrowItem itemWithIcon:@"" title:@"我的身份" destcVc:nil];
    status.subTitle = @"男孩 资深工作党";
    status.option = nil;
    GTSettingGroup *group0 = [GTSettingGroup initSettingGroupWithItems:@[status] titleForHeader:nil titleForFooter:nil];
    [self.dataList addObject:group0];
    
    GTSettingItem *invite = [GTSettingItem itemWithIcon:@"" title:@"邀请好友使用礼物说"];
    GTSettingLabelItem *grade = [GTSettingLabelItem itemWithIcon:@"" title:@"给我们评分吧"];
    grade.title = @"奖励5积分";
    GTSettingArrowItem *suggest = [GTSettingArrowItem itemWithIcon:@"" title:@"" destcVc:nil];
    GTSettingGroup *group1 = [GTSettingGroup initSettingGroupWithItems:@[invite, suggest] titleForHeader:nil titleForFooter:nil];
    [self.dataList addObject:group1];
    
    GTSettingSwitchItem *like = [GTSettingSwitchItem itemWithIcon:@"" title:@"喜欢礼物到默认礼单"];
    GTSettingSwitchItem *push = [GTSettingSwitchItem itemWithIcon:@"" title:@"接受消息推送"];
    GTSettingSwitchItem *night = [GTSettingSwitchItem itemWithIcon:@"" title:@"夜间模式"];
    GTSettingArrowItem *clear = [GTSettingArrowItem itemWithIcon:@"" title:@"清除缓存"];
    clear.subTitle = @"1.56MB";
    clear.option = nil;
    GTSettingGroup *group2 = [GTSettingGroup initSettingGroupWithItems:@[like, push, night, clear] titleForHeader:nil titleForFooter:nil];
    [self.dataList addObject:group2];
    
    GTSettingArrowItem *about = [GTSettingArrowItem itemWithIcon:@"" title:@"关于礼物说" destcVc:nil];
    GTSettingGroup *group3 = [GTSettingGroup initSettingGroupWithItems:@[about] titleForHeader:nil titleForFooter:nil];
    [self.dataList addObject:group3];
    
    GTSettingArrowItem *recommend = [GTSettingArrowItem itemWithIcon:@"" title:@"推荐应用" destcVc:nil];
    GTSettingGroup *group4 = [GTSettingGroup initSettingGroupWithItems:@[recommend] titleForHeader:@"小礼菌推荐应用" titleForFooter:nil];
    [self.dataList addObject:group4];
}

# pragma mark - Lift Cycle

- (instancetype)init {
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 初始化数据
    [self setupDataList];
    
    
}

#pragma mark - table view delegate

/** 选中右侧箭头 跳转新的控制器 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //1. 取消选中
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //2. 取出模型
    GTSettingGroup *group = self.dataList[indexPath.section];
    GTSettingItem *item = group.items[indexPath.row];
    
    if ([item isKindOfClass:[GTSettingArrowItem class]]) {
        GTSettingArrowItem *arrowItem = (GTSettingArrowItem *)item;
        
        if (arrowItem.destcVcClass) {
            // 创建跳转控制器
            UIViewController *vc = [[arrowItem.destcVcClass alloc] init];
            
            /** 设置跳转控制器的标题 */
            vc.title = arrowItem.title;
            
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        // 判断是否有特殊的操作，有的话就调用
        !arrowItem.option ? : arrowItem.option();
    }
}

#pragma mark - table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataList.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    GTSettingGroup *currentGroup = self.dataList[section];
    
    return currentGroup.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //1. 创建 cell
    GTSettingCell *cell = [GTSettingCell cellWithTableView:tableView];
    
    //2. 取出模型
    GTSettingGroup *group = self.dataList[indexPath.section];
    GTSettingItem *item = group.items[indexPath.row];
    
    //3. 传递模型
    cell.item = item;
    
    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    GTSettingGroup *group = self.dataList[section];
    return group.titleForHeader;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    GTSettingGroup *group = self.dataList[section];
    return group.titleForFooter;
}

@end
